package com.lbs.jiepiaisi.parser;
import com.lbs.jiepiaisi.bean.HistoryTraceBean;
import com.lbs.jiepiaisi.utils.Constant;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * 时间：2017/8/30 10:06
 * 作用：单个历史轨迹解析类
 */

public class AccountHistoryParser {

    public static HistoryTraceBean parser(String jsonString){
        HistoryTraceBean historyTraceBean =new HistoryTraceBean();
        List<HistoryTraceBean.Result>listResult=new ArrayList<HistoryTraceBean.Result>();
        try{
            JSONObject jsonObject=new JSONObject(jsonString);
            //JSON取值
            JSONArray jsonArray=jsonObject.getJSONArray("result");
            for(int i=0;i<jsonArray.length();i++) {
                JSONObject JsonResultItem = jsonArray.getJSONObject(i);
                HistoryTraceBean.Result result=new HistoryTraceBean.Result();
                //经纬度
                double gdLat=JsonResultItem.getDouble("gdLat");
                double gdLng=JsonResultItem.getDouble("gdLng");
                //时间
                String positionTime=JsonResultItem.getString("positionTime");
                //速度
                String speed=JsonResultItem.getString("speed");
                try {
                    String f = JsonResultItem.getString("dir");
                    if(f == null || "null".equals(f)){
                        result.setRotate(0);
                    }else {
                        float rotate = (float) JsonResultItem.getDouble("dir");
                        result.setRotate(rotate);
                    }
                    if(JsonResultItem.getString("satatus") != null && !JsonResultItem.getString("satatus").equals("null")){
                        result.setSatatus(JsonResultItem.getString("satatus"));
                    }else {
                        result.setSatatus("");
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
                result.setSpeed(speed);
                result.setPositionTime(positionTime);
                result.setGdLat(gdLat);
                result.setGdLng(gdLng);
                result.setPostType(JsonResultItem.getString("postType"));

                listResult.add(i, result);
            }
            historyTraceBean.setCode(Constant.STATUS_OK);
            historyTraceBean.setResult(listResult);
        }catch (Exception e){
            historyTraceBean.setCode(Constant.STATUS_NULL);
            e.printStackTrace();
        }
        return historyTraceBean;
    }
}
